/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.web.servlet.admin.tableresto;

import com.supinfo.youfood.entity.Restaurant;
import com.supinfo.youfood.entity.TableResto;
import com.supinfo.youfood.service.RestaurantService;
import com.supinfo.youfood.service.TableRestoService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 *
 * @author TaKeO
 */
@WebServlet(name = "AddTableRestoServlet", urlPatterns = {"/admin/ajouter-table-restaurant"})
public class AddTableRestoServlet extends HttpServlet {
        
    @EJB
    private TableRestoService tableRestoService;
    
    @EJB
    private RestaurantService restoService;
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        List<Restaurant> restos = restoService.getAllResto();
        request.setAttribute("restos", restos);
            
        request.getRequestDispatcher("/jsp/admin/ajouter-table-restaurant.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        TableResto table = new TableResto();
        
        table.setNom(request.getParameter("nom"));
        try
        {
            table.setNombre_personnes(new Integer(request.getParameter("places")));
        }
        catch(Exception e)
        {
            // erreur
        }
        
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();

        Set<ConstraintViolation<TableResto>> constraintViolations = validator.validate(table);

        if (constraintViolations.size() > 0) 
        {
            List<String> errors = new ArrayList<String>();
            
            for (ConstraintViolation<TableResto> contraintes : constraintViolations) 
            {
                errors.add(contraintes.getPropertyPath()+" "+contraintes.getMessage());
            }
            
            request.setAttribute("errors", errors);
            getServletContext().getRequestDispatcher("/jsp/admin/ajouter-table-restaurant.jsp").forward(request, response);
        }
        else
        {
            Restaurant resto = restoService.findRestoById(new Long(request.getParameter("resto")));
            table.setResto(resto);
            tableRestoService.addTableResto(table);
            resto.addTable(table);
            restoService.refreshResto(resto);

            response.sendRedirect("/YouFood/admin/liste-tables-restaurants");
        }
    }

}
